package lichunUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Analyze the parameter setting.
 * @author lichunyang
 */
public class AnalyzeParameter {

	/**
	 * Analyze a string like "-filepath d:\abc"
	 * @param para
	 * @return
	 */
	static public Map<String, String> simAnalyze(String para) {
		Map<String, String> res = new HashMap<String, String>();
		if (para == null)
			return res;

		String[] parts = para.split(" ");
		String key = null;
		String value = "";
		for (int i = 0; i < parts.length; i++) {
			if (parts[i].startsWith("-")) {
				if (key != null)
					res.put(key, value);
				key = parts[i].substring(1);
				value = "";
			} else {
				if (!value.trim().equals(""))
					value += " ";
				if (parts[i].equals(""))
					value += " ";
				value += parts[i];
			}
		}
		if (key != null)
			res.put(key, value);

		return res;

	}

	static public void selftest() {
		//String x = "-a sadf -b eroiu -c asdjf zxc eraw -sad zcve -f d:\\asdf\\java";
		String x = "-a   -b asd";
		Map<String, String> res = simAnalyze(x);
		for (String k : res.keySet()) {
			System.out.printf("%s\t%s%n", k, res.get(k));
		}
		System.out.println("a\tb");
	}
}